package com.ruoyi.zxjd.work.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zxjd.work.mapper.WorkFormMapper;
import com.ruoyi.zxjd.work.mapper.WorkProgressMapper;
import com.ruoyi.zxjd.common.CommonConstants;
import com.ruoyi.zxjd.work.domain.WorkForm;
import com.ruoyi.zxjd.work.domain.WorkProgress;
import com.ruoyi.zxjd.work.service.IWorkFormService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;

/**
 * 填报表单Service业务层处理
 * 
 * @author lcc
 * @date 2021-01-26
 */
@Service
public class WorkFormServiceImpl implements IWorkFormService 
{
    @Autowired
    private WorkFormMapper workFormMapper;
    @Autowired
    private WorkProgressMapper workProgressMapper;

    /**
     * 查询填报表单
     * 
     * @param formId 填报表单ID
     * @return 填报表单
     */
    @Override
    public WorkForm selectWorkFormById(Long formId)
    {
        return workFormMapper.selectWorkFormById(formId);
    }

    /**
     * 查询填报表单列表
     * 
     * @param workForm 填报表单
     * @return 填报表单
     */
    @Override
    public List<WorkForm> selectWorkFormList(WorkForm workForm)
    {
        return workFormMapper.selectWorkFormList(workForm);
    }

    /**
     * 新增填报表单
     * 
     * @param workForm 填报表单
     * @return 结果
     */
    @Override
    public int insertWorkForm(WorkForm workForm)
    {
        workForm.setCreateBy(ShiroUtils.getLoginName());
        workForm.setCreateTime(DateUtils.getNowDate());
        workForm.setUpdateBy(ShiroUtils.getLoginName());
        workForm.setUpdateTime(DateUtils.getNowDate());
        return workFormMapper.insertWorkForm(workForm);
    }

    /**
     * 修改填报表单
     * 
     * @param workForm 填报表单
     * @return 结果
     */
    @Override
    public int updateWorkForm(WorkForm workForm)
    {
        workForm.setUpdateBy(ShiroUtils.getLoginName());
        workForm.setUpdateTime(DateUtils.getNowDate());
        Long progressId = workForm.getProgressId();
        WorkProgress workProgress = workProgressMapper.selectWorkProgressById(progressId);
        workProgress.setStatus(CommonConstants.REPORTED);
        String nowDate = DateUtils.getDate();
        String endDate = DateUtils.parseDateToStr("yyyy-MM-dd", workProgress.getEndDate());
        if (StringUtils.compare(nowDate, endDate)>0) {
            workProgress.setStatus(CommonConstants.OVERDUE_REPORTED);
        }
        workProgressMapper.updateWorkProgress(workProgress);
        //解析填报值并保存到模板表
        // List<JSONObject> list = new ArrayList<>();
        // String formContent = workForm.getFormContent();
        // JSONArray jsonArray = JSON.parseArray(formContent).getJSONObject(0).getJSONArray("celldata");
        //     for (int i = 1; i < jsonArray.size(); i++) {
        //         JSONObject jsonData = new JSONObject();
        //         if (jsonArray.getJSONObject(i) != null) {
        //             JSONObject jsonObject = jsonArray.getJSONObject(i);
        //         }
        //     }
        return workFormMapper.updateWorkForm(workForm);
    }

    /**
     * 删除填报表单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWorkFormByIds(String ids)
    {
        return workFormMapper.deleteWorkFormByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除填报表单信息
     * 
     * @param formId 填报表单ID
     * @return 结果
     */
    @Override
    public int deleteWorkFormById(Long formId)
    {
        return workFormMapper.deleteWorkFormById(formId);
    }

    /**
     * 通过工作进度ID查询填报表单信息
     * 
     * @param progressId 工作进度ID
     * @return 结果
     */
    @Override
	public WorkForm selectWorkFormByprogressId(Long progressId){
        return workFormMapper.selectWorkFormByprogressId(progressId);
    }
}
